package com.lry.lambda;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author JiangJie
 * @create 2020-12-10 12:26
 */
public class LambdaDemo1 {
    @Test
    public void t() {
        TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        TreeSet<Integer> set1 = new TreeSet<>((x, y) -> {
            return x - y;
        });
    }

    @Test
    public void t2() {
        List<Integer> list = Arrays.asList(1,2,3,4,5,6);
        list.stream().filter(it->it>3).limit(2).collect(Collectors.toList()).forEach(System.out::println);
    }

    @Test
    public void t3(){
        Consumer<String> consumer = it-> System.out.println(it);

        consumer.accept("Hello world");
    }

    @Test
    public void t4(){
        Supplier<Integer> supplier = ()->new Random().nextInt(100);
        System.out.println(supplier.get());
    }

    @Test
    public void t5(){
        Predicate<Integer> predicate = it->it>5;
        System.out.println(predicate.test(4));
        System.out.println(predicate.test(6));
    }

    @Test
    public void t6(){
        //接受int，返回string
        Function<Integer,String> function = x->String.valueOf(x);
        String s = function.apply(1);
        System.out.println(s);
    }

    @Test
    public void t7(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,1);
        list.stream().filter(it->it>3).limit(2).collect(Collectors.toList()).forEach(System.out::println);
        System.out.println();
        list.stream().map(x->"str "+String.valueOf(x)).forEach(System.out::println);
        System.out.println();
        System.out.println(list.stream().max(Integer::compareTo).get());
        System.out.println();
        System.out.println(list.stream().anyMatch(it -> it > 7));
        System.out.println(list.stream().anyMatch(it -> it > 5));
        System.out.println(list.stream().allMatch(it -> it > 3));
        System.out.println(list.stream().count());
        System.out.println();
        list.stream().distinct().forEach(System.out::println);
        System.out.println();
        System.out.println(list.stream().findAny().get());
        System.out.println(list.stream().findFirst().get());
        list.stream().sorted().forEach(System.out::println);
    }
}
